package com.mowh.multithreading.thread.per.message;

import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * @author mowh1gd
 * @create 2023/11/23 14:05
 */
public class ScheduledExecutorServiceHost {
    private final Helper helper = new Helper();
    private final ScheduledExecutorService scheduledExecutorService;

    public ScheduledExecutorServiceHost(ScheduledExecutorService scheduledExecutorService) {
        this.scheduledExecutorService = scheduledExecutorService;
    }

    public void request(final int count, final char c) {
        System.out.println("    request(" + count + ", " + c + ")  BEGIN");
        scheduledExecutorService.schedule(//java.util.concurrent.ScheduledExecutorService：隐式创建线程
                new Runnable() {
                    @Override
                    public void run() {
                        helper.handle(count, c);
                    }
                },
                3L,
                TimeUnit.SECONDS
        );
        System.out.println("    request(" + count + ", " + c + ")  END");
    }
}
